package com.xykj.enterprise.wechat.ext.api.contacts.member;

import com.xykj.enterprise.wechat.bean.ext.contacts.member.SimpleUserListVo;
import com.xykj.enterprise.wechat.ext.service.contacts.ContactsUserService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplecache.SimpleCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Function(description = "获取部门成员", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "department_id", type = Type.Integer, description = "部门id", required = true),
        @Parameter(name = "fetch_child", type = Type.Integer, description = "是否递归获取成员", required = false),
})
@Component
public class SimpleList implements Action {

    @Autowired
    private SimpleCache simpleCache;
    @Autowired
    private ContactsUserService contactsUserService;

    @Override
    public Response execute(Request request) throws Exception {

        Integer department_id = request.getInteger("department_id");
        Integer fetch_child = request.getInteger("fetch_child");
        if (fetch_child == null) {
            fetch_child = 0;
        }
        SimpleUserListVo simpleUserListVo = contactsUserService.simpleList(
                request.getString("access_token"), department_id, fetch_child
        );
        if (simpleUserListVo.getErrcode() != 0) {
            return Response.fail(simpleUserListVo.getErrmsg());
        }
        return Response.success().put("data", simpleUserListVo);
    }
}
